/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package business;

import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author stana
 */
public class GpsRecords
{

    private List<GpsRecord> list = new LinkedList<GpsRecord>();
    private final double radius = 6371000;
    private double upMeters = 0;
    private double downMeters = 0;

    public GpsRecords(List<GpsRecord> list)
    {
        this.list = list;
    }

    public List<XYRecord> getAltitudeList()
    {
        LinkedList<XYRecord> ret = new LinkedList<XYRecord>();
        double distance = 0;
        if (!list.isEmpty())
        {
            GpsRecord previous = list.get(0);
            for (GpsRecord actual : list)
            {
                distance += calculateDistance(previous, actual);
                ret.add(new XYRecord(distance, actual.alt));
                previous = actual;
            }
        }
        return ret;
    }

    public List<XYRecord> getSpeedList()
    {

        LinkedList<XYRecord> ret = new LinkedList<XYRecord>();
        double distance = 0;
        int lastHundred = 1;
        int modulo=0;
        double averageSpeed=0;
        int counter=0;
        if (!list.isEmpty())
        {
            GpsRecord previous = list.get(0);
            for (GpsRecord actual : list)
            {
                distance += calculateDistance(actual, previous);
                modulo=(int) (distance/100);
                averageSpeed+=actual.speed;
                if(modulo==lastHundred)//nova stovka
                {
                   ret.add(new XYRecord(distance, (1/(averageSpeed/counter))*(50.0/3.0) ));
                   averageSpeed=0;
                   counter=0;
                   lastHundred++;
                }
                else
                {
                    counter++;
                }
//               if(actual.speed==0)
//               {
//                   actual.speed=0.001;
//               }
                
                previous = actual;
            }
            ret.add(new XYRecord(distance, averageSpeed/counter * 3.6));

        }
        return ret;
    }

    private double getTime(String dateTime)
    {
        double ret = 0;
        int i = dateTime.indexOf("T");
        dateTime = dateTime.substring(++i, dateTime.length() - 1);
        String array[] = dateTime.split(":");
        ret = (Double.valueOf(array[0]) * 3600) + (Double.valueOf(array[1]) * 60)
                + (Double.valueOf(array[2]));
        return ret;
    }

    private double calculateDistance(GpsRecord firstPoint, GpsRecord secondPoint)
    {
        double lat1 = Math.toRadians(firstPoint.lat);
        double lat2 = Math.toRadians(secondPoint.lat);
        double dLat = Math.abs(lat1 - lat2);
        double dLng = Math.toRadians(firstPoint.lng - secondPoint.lng);
        double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
                + Math.sin(dLng / 2) * Math.sin(dLng / 2) * Math.cos(lat1) * Math.cos(lat2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        double d = radius * c;
        return d;
    }

    public double getDistance()
    {
        double distance = 0;
        if (!list.isEmpty())
        {
            GpsRecord previous = list.get(0);
            for (GpsRecord actual : list)
            {
                distance += calculateDistance(previous, actual);
                previous = actual;
            }
        }
        return distance;
    }

    public double getTotalTime()
    {
        double t1 = 0;
        double t2 = 0;
        if (!list.isEmpty())
        {
            t1 = getTime(list.get(0).time);
            t2 = getTime(list.get(list.size() - 1).time);
        }
        return t2 - t1;
    }

    public double getUpMeters()
    {

        double deltaM;

        if (!list.isEmpty())
        {
            GpsRecord previous = list.get(0);
            for (GpsRecord actual : list)
            {
                deltaM = actual.alt - previous.alt;
                if (deltaM >= 0)
                {
                    upMeters += deltaM;
                } else
                {
                    downMeters += deltaM;
                }
                previous = actual;
            }
            downMeters *= -1;
        }
        return upMeters;
    }

    public double getDownMeters()
    {
        double deltaM;
        if (downMeters == 0)//vhodnejsi volat po getUpMeters
        {
            if (!list.isEmpty())
            {
                GpsRecord previous = list.get(0);
                for (GpsRecord actual : list)
                {
                    deltaM = actual.alt - previous.alt;
                    if (deltaM <= 0)
                    {
                        downMeters += deltaM;
                    }
                    previous = actual;
                }
                downMeters *= -1;
            }
        }
        return downMeters;
    }
}
